package com.baizhi.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @Author：请允悲
 * @Slogan：致敬大师，致敬未来的me
 * @Package：com.baizhi
 * @Project：springbootjwt
 * @name：utils
 * @Date：2023/9/20 15:44
 * @Filename：utils
 */
public class JWTUtils {
    private static String SIGNATURE = "token!@#$%^7890";

    /**
     * 生成token
     * @param map //传入payload
     * @return 返回token
     */
    public static String getToken(Map<String,String> map){
        //创建jwtbuilder
        JWTCreator.Builder builder = JWT.create();
        //payload
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.SECOND,7000);//默认时间7天
         String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGNATURE));
        return token;
    }

//    /**
//     * 验证token
//     * @param token
//     */
//    public static void verify(String token){
//        JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
//    }

    /**
     * 获取token中payload 验证token
     * @param token
     * @return
     */
    public static DecodedJWT verify(String token){
        return JWT.require(Algorithm.HMAC256(SIGNATURE)).build().verify(token);
    }
}

